package com.example.demo.contoller;

import com.example.demo.model.JsonData;
import com.example.demo.model.ProductDO;
import com.example.demo.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商品业务增删改查
 */
@RestController
@RequestMapping("/api/product")
public class ProductController {

    /**
     * 业务层对象
     */
    @Autowired
    private ProductService productService;

    /**
     * 增加商品
     * @param productDO
     * @return
     */
    @RequestMapping("add")
    public JsonData insert(@RequestBody ProductDO productDO){
        int insert = productService.insert(productDO);
        return JsonData.buildSuccess(insert);
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @RequestMapping("del")
    public JsonData delete( int id){
        int i = productService.delById(id);
        return JsonData.buildSuccess(i);
    }

    /**
     * 修改商品 根据id
     * @param productDO
     * @return
     */
    @RequestMapping("update")
    public JsonData update(@RequestBody ProductDO productDO){
        productService.updateById(productDO);
        return JsonData.buildSuccess();
    }

    /**
     * 查询单条商品
     * @param id
     * @return
     */

    @RequestMapping("query_one")
    public JsonData queryOne( int id){
        return JsonData.buildSuccess(productService.getById(id));
    }

    /**
     * 分页查询
     * @param pageNumber
     * @param pageSize
     * @return
     */

    @RequestMapping("query_size")
    public JsonData querySize(String pageNumber,String pageSize){
        return JsonData.buildSuccess(productService.getListPage(pageNumber,pageSize));
    }
}
